<!DOCTYPE html>
<!--

// Copyright 2011 Traceur Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

-->
<html>
<head>
<title></title>
<script src="../../third_party/closure-library/closure/goog/base.js"></script>
<script>
goog.require('goog.testing.jsunit');
</script>
<script src="../../bin/traceur.js"></script>
<script src="../test-utils.js"></script>
<script src="../test-list.js"></script>
</head>
<body>
<script>


/**
 * Given a path like "feature/Foo/Bar" makes a function name to test that
 * feature: "testFooBar".
 */
function makeTestName(path) {
  var pathStart = 'feature/';
  var start = path.indexOf(pathStart);

  var name = path.substring(start >= 0 ? start + pathStart.length : 0);
  name = name.replace('/', '');
  name = name.replace('.js', '');

  return 'test' + name;
}

function runCode(code, name) {
  'use strict';
  
  var completed = false;
  try {
    ('global', eval)(code);
    completed = true;
  } finally {
    if (!completed) {
      fail('Error running compiled output for : ' + name);
    }
  }
}

function evalScript(urlOptions, url) {
  'use strict';

  var name = url;
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, false);
  xhr.send();
  var source = xhr.responseText;

  // For each Traceur .js file, we'll create a top-level "test___" function
  // that compiles and evaluates it. JsUnit will then find and run those
  // functions for us.
  // We eval each file in the scope of its own function so we don't have to
  // worry about name clashes across the different feature scripts.
  var testName = makeTestName(name);
  window[testName] = function() {

    traceur.options.debug = true;
    traceur.options.freeVariableChecker = true;
    traceur.options.validate = true;

    var options = parseProlog(source);
    var skip = options.skip;
    var shouldCompile = options.shouldCompile;
    var expectedErrors = options.expectedErrors;

    try {
      var reporter = new traceur.util.TestErrorReporter();
      var sourceFile = new traceur.syntax.SourceFile(name, source);
      var tree = traceur.codegeneration.Compiler.compileFile(reporter,
                                                             sourceFile,
                                                             url);
      var code = traceur.outputgeneration.TreeWriter.write(tree);

      if (!shouldCompile) {
        assertTrue('Expected error compiling ' + name + ', but got none.',
                   reporter.hadError());

        var missingExpectations = expectedErrors.forEach(function(expected) {
          assertTrue('Missing expected error: ' + expected,
                     reporter.hasMatchingError(expected));
        });

        skip = true;
      }

      var CloneTreeTransformer = traceur.codegeneration.CloneTreeTransformer;

      if (!skip) {
          if (reporter.hadError()) {
            fail('Error compiling ' + name + '.\n' +
                 reporter.errors.join('\n'));
            return;
          }
          if (urlOptions.testClone === 'true') {
            var clone = CloneTreeTransformer.cloneTree(tree);
            code = traceur.outputgeneration.TreeWriter.write(tree);
            var cloneCode = traceur.outputgeneration.TreeWriter.write(clone);
            assertEquals(code, cloneCode);
          } else {
            // Script compiled, so run it.
            runCode(code, name);
          }
      }
    } finally {
      traceur.options.reset();
    }
  }
}

function optionsOnURL() {
  var params = window.location.search.substring(1);
  var nameValuePairs = params.split('&');
  var options = {};
  nameValuePairs.forEach(function(pair){
    var segments = pair.split('=');
    options[segments[0]] = segments[1];
  });
  return options;
}

// Run all tests in testList.
Array.prototype.forEach.call(testList, evalScript.bind(null, optionsOnURL()));

</script>
</body>
</html>
